package com.mamahao.vmnetframework.utils;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.mamahao.vmnetframework.R;

/**
 * 作者 by alone
 * on 2017/6/11.
 */
public class ProgressBarUtils {

    private static final int progressId = R.id.progressbar_id;

    public static void showProgressBar(Activity activity){
        if(null == activity){
            return;
        }

        ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
        if(null != viewGroup){
            if(null != viewGroup.findViewById(progressId)){
                View progressBar = viewGroup.findViewById(progressId);
                progressBar.setVisibility(View.VISIBLE);
            }else{
                View view = View.inflate(activity, R.layout.layout_progressbar, null);
                view.setId(progressId);
                viewGroup.addView(view);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER;
                view.setLayoutParams(params);
                view.setVisibility(View.VISIBLE);
            }
        }
    }

    public static void hideProgressBar(Activity activity){
        if(null == activity){
            return;
        }
        ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
        if(null != viewGroup){
            if(null != viewGroup.findViewById(progressId)){
                View progressBar = viewGroup.findViewById(progressId);
                progressBar.setVisibility(View.GONE);
            }
        }
    }

    public static void showProgressBar(Fragment fragment){
        try{
            if (null != fragment && fragment.isAdded() && null != fragment.getActivity()) {
                ViewGroup viewGroup = (ViewGroup) fragment.getView();
                if(null != viewGroup && (viewGroup instanceof FrameLayout || viewGroup instanceof RelativeLayout)){
                    if(null != viewGroup.findViewById(progressId)){
                        View progressBar = viewGroup.findViewById(progressId);
                        progressBar.setVisibility(View.VISIBLE);
                    }else{
                        View view = View.inflate(fragment.getActivity(), R.layout.layout_progressbar, null);
                        view.setId(progressId);
                        view.setClickable(true);
                        viewGroup.addView(view);
                        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                        params.gravity = Gravity.CENTER;
                        view.setLayoutParams(params);
                        view.setVisibility(View.VISIBLE);
                    }
                }
            }
        }catch (Exception e){}


    }

    public static void hideProgressBar(Fragment fragment){
        try{
            if (null != fragment && fragment.isAdded() && null != fragment.getActivity()) {
                ViewGroup viewGroup = (ViewGroup) fragment.getView();
                if(null != viewGroup){
                    if(null != viewGroup.findViewById(progressId)){
                        View progressBar = viewGroup.findViewById(progressId);
                        progressBar.setVisibility(View.GONE);
                    }
                }
            }
        }catch (Exception e){}
    }

    public static boolean isShowing(Activity activity){
        if(null == activity){
            return false;
        }
        ViewGroup viewGroup = (ViewGroup) activity.getWindow().getDecorView();
        if(null != viewGroup){
            if(null != viewGroup.findViewById(progressId)){
                View progressBar = viewGroup.findViewById(progressId);
                return progressBar.getVisibility() == View.VISIBLE;
            }
        }
        return false;
    }
}
